<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Callback extends CI_Controller {
	
	function __construct(){
		parent::__construct();
	}
	
	function index(){
		//$this->load->model('User_model', 'user');
	}
	
	
	function back_url(){
		$merchantId 	  = $_POST["merchantId"];
		$payNo 	  			= $_POST["payNo"];
		$returnCode 	  = $_POST["returnCode"];
		$message	  		= $_POST["message"];
		$signType      	= $_POST["signType"];
		$type         	= $_POST["type"];
		$version        	= $_POST["version"];
		$amount         = $_POST["amount"];
		$amtItem			  = $_POST["amtItem"];		
		$bankAbbr	  		= $_POST["bankAbbr"];
		$mobile 			  = $_POST["mobile"];
		$orderId			  = $_POST["orderId"];
		$payDate			  = $_POST["payDate"];
		$accountDate    = $_POST["accountDate"];
		$reserved1	  	= $_POST["reserved1"];
		$reserved2	  	= $_POST["reserved2"];
		$status				  = $_POST["status"];
		$orderDate      = $_POST["orderDate"];
		$fee            = $_POST["fee"];
		$vhmac			  	= $_POST["hmac"];
		
		$this->load->model('Payment_model', 'payment');
		$result = $this->payment->get_one(array("nid"=>"sjzf"));
		
		$tmp = unserialize($result['config']);
			
		$signKey        = $tmp['PrivateKey'];
		
		//接收手机支付平台页面通知传递的报文 end
		
		//组装签字符串
		$signData = $merchantId .$payNo.$returnCode .$message
			   .$signType   .$type        .$version    .$amount
			   .$amtItem    .$bankAbbr    .$mobile     .$orderId
			   .$payDate    .$accountDate .$reserved1  .$reserved2
			   .$status     .$orderDate   .$fee;
			   
		//MD5方式签名
		$hmac=MD5sign($signKey,$signData);
		
		//此处000000仅代表程序无错误。订单是否支付成功是以支付结果（status）为准
		if($returnCode!=000000)
		{
			 echo $returnCode.decodeUtf8($message);
		}
		
		if($hmac != $vhmac)
			echo "验签失败";
		else{
			  /*商户在此处业务处理*/			
			$this->load->model('Accountrecharge_model', 'recharge');
			$ret = $this->recharge->onreturn(array("trade_no"=>$orderId));
			$msg = 'succ';
			
			echo "<script>alert('{$msg}');location.href='/account/recharge_log';</script>";
		}
	}
	
	function notifyurl(){
		$merchantId 	 = $_POST["merchantId"];
		$payNo 	  			= $_POST["payNo"];
		$returnCode 	  = $_POST["returnCode"];
		$message	  		= $_POST["message"];
		$signType       	= $_POST["signType"];
		$type         	= $_POST["type"];
		$version        	= $_POST["version"];
		$amount         = $_POST["amount"];
		$amtItem			  = $_POST["amtItem"];		
		$bankAbbr	  		= $_POST["bankAbbr"];
		$mobile 			  = $_POST["mobile"];
		$orderId			  = $_POST["orderId"];
		$payDate			  = $_POST["payDate"];
		$accountDate    = $_POST["accountDate"];
		$reserved1	  	= $_POST["reserved1"];
		$reserved2	  	= $_POST["reserved2"];
		$status				  = $_POST["status"];
		$payType        = $_POST["payType"];
		$orderDate      = $_POST["orderDate"];
		$fee            = $_POST["fee"];
		$vhmac			  	= $_POST["hmac"];
		//接收手机支付平台后台通知数据end
		$this->load->model('Payment_model', 'payment');
		$result = $this->payment->get_one(array("nid"=>"sjzf"));
		
		$tmp = unserialize($result['config']);
			
		$signKey        = $tmp['PrivateKey'];
		if($returnCode!=000000)
		{
			//此处表示后台通知产生错误
			echo $returnCode.decodeUtf8($message);
			exit();
		}
		
		$signData = $merchantId .$payNo       .$returnCode .$message
		.$signType   .$type        .$version    .$amount
		.$amtItem    .$bankAbbr    .$mobile     .$orderId
		.$payDate    .$accountDate .$reserved1  .$reserved2
		.$status     .$orderDate  .$fee;
		$hmac=MD5sign($signKey,$signData);	
		if($hmac != $vhmac)
			//此处无法信息数据来自手机支付平台
			echo "验签失败";
		else{
			//商户在此处做业务处理，处理完毕必须响应SUCCESS
			$this->load->model('Accountrecharge_model', 'recharge');
			$ret = $this->recharge->onreturn(array("trade_no"=>$orderId));
			echo "SUCCESS";
		}
		
	}
	
	function hcreturn(){
		$BillNo = $_REQUEST['BillNo'];
		$Amount = $_REQUEST['Amount'];
		$Succeed = $_REQUEST['Succeed'];
		$Result = $_REQUEST['Result'];
		$MD5info = strtoupper($_REQUEST['MD5info']);
		
		$this->load->model('Payment_model', 'payment');
		$result = $this->payment->get_one(array("nid"=>"huichao"));
		$tmp = unserialize($result['config']);
		$MD5key = $tmp['PrivateKey'];
		
		$signature_1ocal = strtoupper(md5($BillNo.$Amount.$Succeed.$MD5key));
		if($signature_1ocal == $MD5info){
			if ($Succeed == 88){
				$this->load->model('Accountrecharge_model', 'recharge');
					$ret = $this->recharge->onreturn(array("trade_no"=>$BillNo));
					$msg = 'succ';
			}else{
				$msg = 'error';
			}
		}else{
			$msg = 'errordata';
		}
		echo "<script>alert('{$msg}');location.href='/account/recharge_log';</script>";
	}
	
	function hcnotify(){
		$BillNo = $_REQUEST['BillNo'];
		$Amount = $_REQUEST['Amount'];
		$Succeed = $_REQUEST['Succeed'];
		$Result = $_REQUEST['Result'];
		$MD5info = strtoupper($_REQUEST['MD5info']);
		
		$this->load->model('Payment_model', 'payment');
		$result = $this->payment->get_one(array("nid"=>"huichao"));
		$tmp = unserialize($result['config']);
		$MD5key = $tmp['PrivateKey'];
		
		$signature_1ocal = strtoupper(md5($BillNo.$Amount.$Succeed.$MD5key));
		if($signature_1ocal == $MD5info){
			if ($Succeed == 88){
				$this->load->model('Accountrecharge_model', 'recharge');
					$ret = $this->recharge->onreturn(array("trade_no"=>$BillNo));
					echo 'ok';
			}else{
				echo 'error';
			}
		}else{
			echo 'errordata';
		}
	}
	
	function getreturn(){
		if (isset($_REQUEST['ipsbillno']) && $_REQUEST['ipsbillno']!=""){//环迅支付
			$billno = $_GET['billno'];
			$amount = $_GET['amount'];
			$mydate = $_GET['date'];
			$succ = $_GET['succ'];
			$msg = $_GET['msg'];
			$attach = $_GET['attach'];
			$ipsbillno = $_GET['ipsbillno'];
			$retEncodeType = $_GET['retencodetype'];
			$currency_type = $_GET['Currency_type'];
			$signature = $_GET['signature'];
			$content = 'billno'.$billno.'currencytype'.$currency_type.'amount'.$amount.'date'.$mydate.'succ'.$succ.'ipsbillno'.$ipsbillno.'retencodetype'.$retEncodeType;
			
			$this->load->model('Payment_model', 'payment');
			$result = $this->payment->get_one(array("nid"=>"ips"));
			
			$tmp = unserialize($result['config']);
			$signature_1ocal = md5($content . $tmp['PrivateKey']);
			if ($signature_1ocal == $signature){
				if ($succ == 'Y'){
					$this->load->model('Accountrecharge_model', 'recharge');
					$ret = $this->recharge->onreturn(array("trade_no"=>$billno));
					$msg = 'succ';
				}else{
					$msg = 'error';
				}
			}else{
				$msg = 'errordata';
			}
			echo "<script>alert('{$msg}');location.href='/account/recharge_log';</script>";
		}
		elseif(isset($_REQUEST['TransID']) && $_REQUEST['TransID']!=""){//宝付
			$_MerchantID=$_REQUEST['MerchantID'];//商户号
			$_TransID =$_REQUEST['TransID'];//流水号
			$_Result=$_REQUEST['Result'];//支付结果(1:成功,0:失败)
			$_resultDesc=$_REQUEST['resultDesc'];//支付结果描述
			$_factMoney=$_REQUEST['factMoney'];//实际成交金额
			$_additionalInfo=$_REQUEST['additionalInfo'];//订单附加消息
			$_SuccTime=$_REQUEST['SuccTime'];//交易成功时间
			$_Md5Sign=$_REQUEST['Md5Sign'];//md5签名
			$_result = $_REQUEST['Result'];
			$this->load->model('Payment_model', 'payment');
			$result = $this->payment->get_one(array("nid"=>"baofoo"));
			$tmp = unserialize($result['config']);
			//$_Md5Key=$result['fields']['PrivateKey']['value'];
			$_WaitSign=md5($_MerchantID.$_TransID.$_Result.$_resultDesc.$_factMoney.$_additionalInfo.$_SuccTime.$tmp['PrivateKey']);
			if ($_Md5Sign == $_WaitSign) {
				if ($_result == '1'){
					$this->load->model('Accountrecharge_model', 'recharge');
					$ret = $this->recharge->onreturn(array("trade_no"=>$_TransID));
					$msg = 'succ';
				}else{
					$msg = 'error';
				}
			} else {
				$msg = 'errordata';
			} 
			echo "<script>alert('{$msg}');location.href='/account/recharge_log';</script>";
		}
		elseif(isset($_POST['v_oid']) && $_POST['v_oid']!=""){//网银在线
			$v_oid     =trim($_POST['v_oid']);       // 商户发送的v_oid定单编号   
			$v_pmode   =trim($_POST['v_pmode']);    // 支付方式（字符串）   
			$v_pstatus =trim($_POST['v_pstatus']);   //  支付状态 ：20（支付成功）；30（支付失败）
			$v_pstring =trim($_POST['v_pstring']);   // 支付结果信息 ： 支付完成（当v_pstatus=20时）；失败原因（当v_pstatus=30时,字符串）； 
			$v_amount  =trim($_POST['v_amount']);     // 订单实际支付金额
			$v_moneytype  =trim($_POST['v_moneytype']); //订单实际支付币种    
			$remark1   =trim($_POST['remark1' ]);      //备注字段1
			$remark2   =trim($_POST['remark2' ]);     //备注字段2
			$v_md5str  =trim($_POST['v_md5str' ]);   //拼凑后的MD5校验值  
			
			$this->load->model('Payment_model', 'payment');
			$result = $this->payment->get_one(array("nid"=>"chinabank"));
			$tmp = unserialize($result['config']);
			
			$md5string=strtoupper(md5($v_oid.$v_pstatus.$v_amount.$v_moneytype.$tmp['PrivateKey']));
			
			if ($v_md5str==$md5string){
				if($v_pstatus=="20"){
					$this->load->model('Accountrecharge_model', 'recharge');
					$ret = $this->recharge->onreturn(array("trade_no"=>$v_oid));
					$msg = 'succ';
				}else{
					$msg = 'error';
				}
			}else{
				$msg = 'error_data';
			}
			echo "<script>alert('{$msg}');location.href='/account/recharge_log';</script>";
		}
	}
	
	function getnotify(){
		if (isset($_REQUEST['ipsbillno']) && $_REQUEST['ipsbillno']!=""){//环迅
			$billno = $_GET['billno'];
			$amount = $_GET['amount'];
			$mydate = $_GET['date'];
			$succ = $_GET['succ'];
			$msg = $_GET['msg'];
			$attach = $_GET['attach'];
			$ipsbillno = $_GET['ipsbillno'];
			$retEncodeType = $_GET['retencodetype'];
			$currency_type = $_GET['Currency_type'];
			$signature = $_GET['signature'];
			$content = 'billno'.$billno.'currencytype'.$currency_type.'amount'.$amount.'date'.$mydate.'succ'.$succ.'ipsbillno'.$ipsbillno.'retencodetype'.$retEncodeType;
			
			$this->load->model('Payment_model', 'payment');
			$result = $this->payment->get_one(array("nid"=>"ips"));
			
			$tmp = unserialize($result['config']);
			$signature_1ocal = md5($content . $tmp['PrivateKey']);
			if ($signature_1ocal == $signature){
				if ($succ == 'Y'){
					$this->load->model('Accountrecharge_model', 'recharge');
					$ret = $this->recharge->onreturn(array("trade_no"=>$billno));
					echo ("ipscheckok");
				}else{
					echo ("error");
				}
			}else{
				echo ("Fail");
			}
		}
		elseif(isset($_REQUEST['TransID']) && $_REQUEST['TransID']!=""){//宝付
			$_MerchantID=$_REQUEST['MerchantID'];//商户号
			$_TransID =$_REQUEST['TransID'];//流水号
			$_Result=$_REQUEST['Result'];//支付结果(1:成功,0:失败)
			$_resultDesc=$_REQUEST['resultDesc'];//支付结果描述
			$_factMoney=$_REQUEST['factMoney'];//实际成交金额
			$_additionalInfo=$_REQUEST['additionalInfo'];//订单附加消息
			$_SuccTime=$_REQUEST['SuccTime'];//交易成功时间
			$_Md5Sign=$_REQUEST['Md5Sign'];//md5签名
			$_result = $_REQUEST['Result'];
			$this->load->model('Payment_model', 'payment');
			$result = $this->payment->get_one(array("nid"=>"baofoo"));
			
			$tmp = unserialize($result['config']);
			$_WaitSign=md5($_MerchantID.$_TransID.$_Result.$_resultDesc.$_factMoney.$_additionalInfo.$_SuccTime.$tmp['PrivateKey']);
			if ($_Md5Sign == $_WaitSign) {
				if ($_result == '1'){
					$this->load->model('Accountrecharge_model', 'recharge');
					$ret = $this->recharge->onreturn(array("trade_no"=>$_TransID));
					echo ("ok");
				}else{
					echo ("Error");
				}
			} else {
				echo ("Fail");
			} 
		}
		elseif(isset($_POST['v_oid']) && $_POST['v_oid']!=""){//网银在线
			$v_oid     =trim($_POST['v_oid']);       // 商户发送的v_oid定单编号   
			$v_pmode   =trim($_POST['v_pmode']);    // 支付方式（字符串）   
			$v_pstatus =trim($_POST['v_pstatus']);   //  支付状态 ：20（支付成功）；30（支付失败）
			$v_pstring =trim($_POST['v_pstring']);   // 支付结果信息 ： 支付完成（当v_pstatus=20时）；失败原因（当v_pstatus=30时,字符串）； 
			$v_amount  =trim($_POST['v_amount']);     // 订单实际支付金额
			$v_moneytype  =trim($_POST['v_moneytype']); //订单实际支付币种    
			$remark1   =trim($_POST['remark1' ]);      //备注字段1
			$remark2   =trim($_POST['remark2' ]);     //备注字段2
			$v_md5str  =trim($_POST['v_md5str' ]);   //拼凑后的MD5校验值  
			
			$this->load->model('Payment_model', 'payment');
			$result = $this->payment->get_one(array("nid"=>"chinabank"));
			$tmp = unserialize($result['config']);
			
			$md5string=strtoupper(md5($v_oid.$v_pstatus.$v_amount.$v_moneytype.$tmp['PrivateKey']));
			
			if ($v_md5str==$md5string){
				if($v_pstatus=="20"){
					$this->load->model('Accountrecharge_model', 'recharge');
					$ret = $this->recharge->onreturn(array("trade_no"=>$v_oid));
				}
				echo "ok";
			}else{
				echo "error";
			}
			//echo "<script>alert('{$msg}');location.href='/account/recharge_log';</script>";
		}
	}
	
	function GetFields(){
         return array(
                'member_id'=>array(
                        'label'=>'客户号',
                        'type'=>'string'
                    ),
                'PrivateKey'=>array(
                        'label'=>'私钥',
                        'type'=>'string',
                )
            );
    }
	
	function backurl(){
		$requestId = $_REQUEST['requestId'];
		$resultSignature = $_REQUEST['resultSignature'];
		$payId = $_REQUEST['payId'];
		$fiscalDate = $_REQUEST['fiscalDate'];
		$description = $_REQUEST['description'];
		
		$sbOld = "";
		$sbOld = $sbOld . $requestId;
		$sbOld = $sbOld . $payId;
		$sbOld = $sbOld . $fiscalDate;
		$sbOld = $sbOld . $description;
		
		$this->load->model('Payment_model', 'payment');
		$result = $this->payment->get_one(array("nid"=>"fengfu"));
			
		$tmp = unserialize($result['config']);
		$Md5key = $tmp['PrivateKey'];
			
		$signatrue = HmacMd5($sbOld, $Md5key);
		
		if($resultSignature == $signatrue){
			//校验通过开始处理订单
			$this->load->model('Accountrecharge_model', 'recharge');
			$ret = $this->recharge->onreturn(array("trade_no"=>$requestId));
			echo "<script>alert('支付成功');location.href='/account/recharge_log.html';</script>";
				
		}else{
			echo("<script>alert('Md5CheckFail');location.href='/account/recharge_log.html';</script>");//MD5校验失败，订单信息不显示
		}	
			
		
		
		
	}
	
	function noticeurl(){
		$requestId = $_REQUEST['requestId'];
		$resultSignature = $_REQUEST['resultSignature'];
		$payId = $_REQUEST['payId'];
		$fiscalDate = $_REQUEST['fiscalDate'];
		$description = $_REQUEST['description'];
		
		$sbOld = "";
		$sbOld = $sbOld . $requestId;
		$sbOld = $sbOld . $payId;
		$sbOld = $sbOld . $fiscalDate;
		$sbOld = $sbOld . $description;
		
		$this->load->model('Payment_model', 'payment');
		$result = $this->payment->get_one(array("nid"=>"fengfu"));
			
		$tmp = unserialize($result['config']);
		$Md5key = $tmp['PrivateKey'];
		
		$signatrue = HmacMd5($sbOld, $Md5key);
				
		
		if ($resultSignature == $signatrue) {
			$this->load->model('Accountrecharge_model', 'recharge');
			$ret = $this->recharge->onreturn(array("trade_no"=>$requestId));
			echo ("ok");//全部正确了输出OK
		} else {
			echo("Md5CheckFail'");//MD5校验失败
		   //处理想处理的事情
		} 
	}
	
	function ldreturl(){
		require_once (APPPATH.'libraries/liandong/plat2Mer.php');
		require_once (APPPATH.'libraries/liandong/mer2Plat.php');
		
		$data = new HashMap ();
		$data->put ( "service", $_REQUEST ['service'] );
		$data->put ( "charset", $_REQUEST ['charset'] );
		$data->put ( "mer_id", $_REQUEST ['mer_id'] );
		$data->put ( "sign_type", $_REQUEST ['sign_type'] );
		$data->put ( "order_id", $_REQUEST ['order_id'] );
		$data->put ( "mer_date", $_REQUEST ['mer_date'] );
		$data->put ( "trade_no", $_REQUEST ['trade_no'] );
		if($_REQUEST ['goods_id']!=null)
		$data->put ( "goods_id", $_REQUEST ['goods_id'] );
		$data->put ( "pay_date", $_REQUEST ['pay_date'] );
		$data->put ( "amount", $_REQUEST ['amount'] );
		$data->put ( "amt_type", $_REQUEST ['amt_type'] );
		$data->put ( "pay_type", $_REQUEST ['pay_type'] );
		if($_REQUEST ['media_id']!=null)
		$data->put ( "media_id", $_REQUEST ['media_id'] );
		if($_REQUEST ['media_type']!=null)
		$data->put ( "media_type", $_REQUEST ['media_type'] );
		$data->put ( "settle_date", $_REQUEST ['settle_date'] );
		if($_REQUEST ['mer_priv']!=null)
		$data->put ( "mer_priv", $_REQUEST ['mer_priv'] );
		$data->put ( "trade_state", $_REQUEST ['trade_state'] );
		$data->put ( "pay_seq", $_REQUEST ['pay_seq'] );
		if($_REQUEST ['error_code']!=null)
		$data->put ( "error_code", $_REQUEST ['error_code'] );
		if($_REQUEST ['gate_id']!=null)
		$data->put ( "gate_id", $_REQUEST ['gate_id'] );
		if($_REQUEST ['identity_type']!=null)
		$data->put ( "identity_type", $_REQUEST ['identity_type'] );
		if($_REQUEST ['identity_code']!=null)
		$data->put ( "identity_code", $_REQUEST ['identity_code'] );
		if($_REQUEST ['card_holder']!=null)
		$data->put ( "card_holder", $_REQUEST ['card_holder'] );
		if($_REQUEST ['last_four_cardid']!=null)
		$data->put ( "last_four_cardid", $_REQUEST ['last_four_cardid'] );
		if($_REQUEST ['usr_busi_agreement_id']!=null)
		$data->put ( "usr_busi_agreement_id", $_REQUEST ['usr_busi_agreement_id'] );
		if($_REQUEST ['usr_pay_agreement_id']!=null)
		$data->put ( "usr_pay_agreement_id", $_REQUEST ['usr_pay_agreement_id'] );
		$data->put ( "version", $_REQUEST ['version'] );
		$data->put ( "sign", $_REQUEST ['sign'] );
				
		//获取请求数据
		$service = $data->get ( "service" );
		$charset = $data->get ( "charset" );
		$mer_id = $data->get ( "mer_id" );
		$sign_type = $data->get ( "sign_type" );
		$order_id = $data->get ( "order_id" );
		$mer_date = $data->get ( "mer_date" );
		$trade_no = $data->get ( "trade_no" );
		$goods_id = $data->get ( "goods_id" );
		$pay_date = $data->get ( "pay_date" );
		$amount = $data->get ( "amount" );
		$amt_type = $data->get ( "amt_type" );
		$pay_type = $data->get ( "pay_type" );
		$media_id = $data->get ( "media_id" );
		$media_type = $data->get ( "media_type" );
		$settle_date = $data->get ( "settle_date" );
		$mer_priv = $data->get ( "mer_priv" );
		$trade_state = $data->get ( "trade_state" );
		$pay_seq = $data->get ( "pay_seq" );
		$error_code = $data->get ( "error_code" );
		$gate_id = $data->get ( "gate_id" );
		$identity_type = $data->get ( "identity_type" );
		$identity_code = $data->get ( "identity_code" );
		$card_holder = $data->get ( "card_holder" );
		$last_four_cardid = $data->get ( "last_four_cardid" );
		$usr_busi_agreement_id = $data->get ( "usr_busi_agreement_id" );
		$usr_pay_agreement_id = $data->get ( "usr_pay_agreement_id" );
		$version = $data->get ( "version" );
		$sign = $data->get ( "sign" );
		
		$resData = new HashMap ();
		try{
			//获取UMPAY平台请求商户的支付结果通知数据,并对请求数据进行验签
			//如验证平台签名正确，即应响应UMPAY平台返回码为0000。【响应返回码代表通知是否成功，和通知的交易结果（支付失败、支付成功）无关】
			//验签支付结果通知 如验签成功，则返回ret_code=0000
			$reqData = PlatToMer::getNotifyRequestData ( $data );
			$this->load->model('Accountrecharge_model', 'recharge');
			$ret = $this->recharge->onreturn(array("trade_no"=>$order_id));
			$resData->put("ret_code","0000");
			echo "<script>alert('支付成功');location.href='/account/recharge_log.html';</script>";
		} catch (Exception $e){
			//如果验签失败，则抛出异常，返回ret_code=1111
			$resData->put("ret_code","1111");
			echo("<script>alert('验证签名发生异常');location.href='/account/recharge_log.html';</script>");//MD5校验失败，订单信息不显示
			
		}
		
		$resData = new HashMap ();
		
		if($mer_id == $member_id){
			//校验通过开始处理订单
			if($ret_code == '0000'){
				$this->load->model('Accountrecharge_model', 'recharge');
				$ret = $this->recharge->onreturn(array("trade_no"=>$requestId));
				echo "<script>alert('支付成功');location.href='/account/recharge_log.html';</script>";
			}else{
				echo("<script>alert('fail');location.href='/account/recharge_log.html';</script>");//MD5校验失败，订单信息不显示
			}
			
				
		}else{
			echo("<script>alert('MEMBERFail');location.href='/account/recharge_log.html';</script>");//MD5校验失败，订单信息不显示
		}	
		
	}
	
	function ldnotifyurl(){
		require_once (APPPATH.'libraries/liandong/plat2Mer.php');
		require_once (APPPATH.'libraries/liandong/mer2Plat.php');
		
		$data = new HashMap ();
		$data->put ( "service", $_REQUEST ['service'] );
		$data->put ( "charset", $_REQUEST ['charset'] );
		$data->put ( "mer_id", $_REQUEST ['mer_id'] );
		$data->put ( "sign_type", $_REQUEST ['sign_type'] );
		$data->put ( "order_id", $_REQUEST ['order_id'] );
		$data->put ( "mer_date", $_REQUEST ['mer_date'] );
		$data->put ( "trade_no", $_REQUEST ['trade_no'] );
		if($_REQUEST ['goods_id']!=null)
		$data->put ( "goods_id", $_REQUEST ['goods_id'] );
		$data->put ( "pay_date", $_REQUEST ['pay_date'] );
		$data->put ( "amount", $_REQUEST ['amount'] );
		$data->put ( "amt_type", $_REQUEST ['amt_type'] );
		$data->put ( "pay_type", $_REQUEST ['pay_type'] );
		if($_REQUEST ['media_id']!=null)
		$data->put ( "media_id", $_REQUEST ['media_id'] );
		if($_REQUEST ['media_type']!=null)
		$data->put ( "media_type", $_REQUEST ['media_type'] );
		$data->put ( "settle_date", $_REQUEST ['settle_date'] );
		if($_REQUEST ['mer_priv']!=null)
		$data->put ( "mer_priv", $_REQUEST ['mer_priv'] );
		$data->put ( "trade_state", $_REQUEST ['trade_state'] );
		$data->put ( "pay_seq", $_REQUEST ['pay_seq'] );
		if($_REQUEST ['error_code']!=null)
		$data->put ( "error_code", $_REQUEST ['error_code'] );
		if($_REQUEST ['gate_id']!=null)
		$data->put ( "gate_id", $_REQUEST ['gate_id'] );
		if($_REQUEST ['identity_type']!=null)
		$data->put ( "identity_type", $_REQUEST ['identity_type'] );
		if($_REQUEST ['identity_code']!=null)
		$data->put ( "identity_code", $_REQUEST ['identity_code'] );
		if($_REQUEST ['card_holder']!=null)
		$data->put ( "card_holder", $_REQUEST ['card_holder'] );
		if($_REQUEST ['last_four_cardid']!=null)
		$data->put ( "last_four_cardid", $_REQUEST ['last_four_cardid'] );
		if($_REQUEST ['usr_busi_agreement_id']!=null)
		$data->put ( "usr_busi_agreement_id", $_REQUEST ['usr_busi_agreement_id'] );
		if($_REQUEST ['usr_pay_agreement_id']!=null)
		$data->put ( "usr_pay_agreement_id", $_REQUEST ['usr_pay_agreement_id'] );
		$data->put ( "version", $_REQUEST ['version'] );
		$data->put ( "sign", $_REQUEST ['sign'] );
				
		//获取请求数据
		$service = $data->get ( "service" );
		$charset = $data->get ( "charset" );
		$mer_id = $data->get ( "mer_id" );
		$sign_type = $data->get ( "sign_type" );
		$order_id = $data->get ( "order_id" );
		$mer_date = $data->get ( "mer_date" );
		$trade_no = $data->get ( "trade_no" );
		$goods_id = $data->get ( "goods_id" );
		$pay_date = $data->get ( "pay_date" );
		$amount = $data->get ( "amount" );
		$amt_type = $data->get ( "amt_type" );
		$pay_type = $data->get ( "pay_type" );
		$media_id = $data->get ( "media_id" );
		$media_type = $data->get ( "media_type" );
		$settle_date = $data->get ( "settle_date" );
		$mer_priv = $data->get ( "mer_priv" );
		$trade_state = $data->get ( "trade_state" );
		$pay_seq = $data->get ( "pay_seq" );
		$error_code = $data->get ( "error_code" );
		$gate_id = $data->get ( "gate_id" );
		$identity_type = $data->get ( "identity_type" );
		$identity_code = $data->get ( "identity_code" );
		$card_holder = $data->get ( "card_holder" );
		$last_four_cardid = $data->get ( "last_four_cardid" );
		$usr_busi_agreement_id = $data->get ( "usr_busi_agreement_id" );
		$usr_pay_agreement_id = $data->get ( "usr_pay_agreement_id" );
		$version = $data->get ( "version" );
		$sign = $data->get ( "sign" );
		
		$resData = new HashMap ();
		try{
			//获取UMPAY平台请求商户的支付结果通知数据,并对请求数据进行验签
			//如验证平台签名正确，即应响应UMPAY平台返回码为0000。【响应返回码代表通知是否成功，和通知的交易结果（支付失败、支付成功）无关】
			//验签支付结果通知 如验签成功，则返回ret_code=0000
			$reqData = PlatToMer::getNotifyRequestData ( $data );
			$this->load->model('Accountrecharge_model', 'recharge');
			$ret = $this->recharge->onreturn(array("trade_no"=>$order_id));
			$resData->put("ret_code","0000");
		} catch (Exception $e){
			//如果验签失败，则抛出异常，返回ret_code=1111
			echo("验证签名发生异常" + $e);//MD5校验失败
			$resData->put("ret_code","1111");
		}
	}
	
	function payResult(){
   		$BillNo = $_POST["BillNo"];
		$Amount = $_POST["Amount"];
		$Succeed = $_POST["Succeed"];     
		$MD5info = $_POST["MD5info"];
		$Result = $_POST["Result"];
		$MerNo = $_POST['MerNo'];
		$MerRemark = $_POST['MerRemark'];
		
		$this->load->model('Payment_model', 'payment');
		$result = $this->payment->get_one(array("nid"=>"epay"));
		$tmp = unserialize($result['config']);
		$MD5key = $tmp['PrivateKey'];
		
		$md5sign = getSignatureCallback($MerNo, $BillNo, $Amount, $Succeed, $MD5key);		
		
		//×××××××××××××××××××××××处理返回结果××××××××××××××××××××××××××××××
		if ($MD5info == $md5sign) {
			if ($Succeed == '88') {
				//返回成功，修改订单状态为成功
				$this->load->model('Accountrecharge_model', 'recharge');
				$ret = $this->recharge->onreturn(array("trade_no"=>$BillNo));
				$msg = 'succ';
			}else {
				//其他结果（可根据文档里的结果代码增加处理类型）
		        $msg = 'error';	
			}
		}else{
			$msg = 'errordata';										//验证失败
		}
		
		echo "<script>alert('{$msg}');location.href='/account/recharge_log';</script>";
		
    }
    
    function notifyResult(){
	    $BillNo          =     $_POST["BillNo"];
		$Amount          =     $_POST["Amount"];
		$Succeed         =     $_POST["Succeed"];     
		$MD5info         =     $_POST["MD5info"];
		$Result          =     $_POST["Result"];
		$MerNo           =     $_POST['MerNo'];
		$this->load->model('Payment_model', 'payment');
		$result = $this->payment->get_one(array("nid"=>"epay"));
		$tmp = unserialize($result['config']);
		$MD5key = $tmp['PrivateKey'];
		$MerRemark       =     $_POST['MerRemark'];		//自定义信息返回
				
		$md5sign = getSignatureCallback($MerNo, $BillNo, $Amount, $Succeed, $MD5key);
				
		if ($MD5info == $md5sign) {
		 	if ($Succeed == '88') {
	            $this->load->model('Accountrecharge_model', 'recharge');
				$ret = $this->recharge->onreturn(array("trade_no"=>$BillNo));
				echo 'ok';
	          }else {
	            echo 'error';
	          }
		 }  else {
		     echo 'errordata';
		 }
	  }
}
